package com.zqj.project.common.util;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.enums.SqlLike;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
import com.zqj.project.common.model.RemoteData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.stream.Stream;

/**
 * @Description: StrHelper
 * @Author: 22001010704 Zhang Qijian
 * @CreateTime: 2024/6/12 23:36
 * @Version: 1.0
 */
public final class StrHelper {

    private static final Logger log = LoggerFactory.getLogger(StrHelper.class);

    private StrHelper() {
        throw new IllegalStateException("Utility Class");
    }

    public static String getOrDef(String val, String def) {
        return DefValueHelper.getOrDef(val, def);
    }

    public static boolean isAnyBlank(CharSequence... css) {
        return ObjectUtil.isEmpty(css) || Stream.of(css).anyMatch(CharSequenceUtil::isBlank);
    }

    public static boolean isNoneBlank(CharSequence... css) {
        return !ObjectUtil.isEmpty(css) && Stream.of(css).allMatch(CharSequenceUtil::isNotBlank);
    }

    public static String keywordConvert(String value) {
        if (CharSequenceUtil.isBlank(value)) {
            return "";
        } else {
            value = value.replace("%", "\\\\%");
            value = value.replace("_", "\\\\_");
            return value;
        }
    }

    public static Object keywordConvert(Object value) {
        if (value instanceof String) {
            return keywordConvert(String.valueOf(value));
        } else if (value instanceof RemoteData temp) {
            if (temp.getKey() instanceof String && ObjectUtil.isNotEmpty(temp.getKey())) {
                temp.setKey(keywordConvert(String.valueOf(temp.getKey())));
                return temp;
            } else {
                return value;
            }
        } else {
            return value;
        }
    }

    public static String like(Object value, SqlLike sqlType) {
        return SqlUtils.concatLike(keywordConvert(String.valueOf(value)), sqlType);
    }

    public static RemoteData<String, ?> like(RemoteData<String, ?> temp, SqlLike sqlType) {
        if (!CharSequenceUtil.isEmpty(temp.getKey())) {
            String oldValue = keywordConvert(temp.getKey());
            temp.setKey(SqlUtils.concatLike(oldValue, sqlType));
        }
        return temp;
    }

    public static String fullLike(String value) {
        return like(value, SqlLike.DEFAULT);
    }

}